<template>
  <el-form class="login-form"
           status-icon
           :rules="loginRules"
           ref="loginForm"
           :model="loginForm"
           label-width="0">
<!--    <el-form-item v-if="tenantMode" prop="tenantId">-->
<!--      <el-input size="small"-->
<!--                @keyup.enter.native="handleLogin"-->
<!--                v-model="loginForm.tenantId"-->
<!--                auto-complete="off"-->
<!--                placeholder="请输入租户ID">-->
<!--        <i slot="prefix"-->
<!--           class="el-icon-key"></i>-->
<!--      </el-input>-->
<!--    </el-form-item>-->
    <el-form-item prop="username">
      <el-input size="small"
                @keyup.enter.native="handleLogin"
                v-model="loginForm.username"
                auto-complete="off"
                placeholder="请输入账号">
        <i slot="prefix"
           class="el-icon-user"></i>
      </el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input size="small"
                @keyup.enter.native="handleLogin"
                :type="passwordType"
                v-model="loginForm.password"
                auto-complete="off"
                placeholder="请输入密码">
        <i class="el-icon-view el-input__icon"
           slot="suffix"
           @click="showPassword"></i>
        <i slot="prefix"
           class="el-icon-lock"></i>
      </el-input>
    </el-form-item>
    <el-form-item prop="code" v-if="captchaEnabled">
      <el-row :span="24">
        <el-col :span="16">
          <el-input size="small"
                    @keyup.enter.native="handleLogin"
                    v-model="loginForm.code"
                    auto-complete="off"
                    placeholder="请输入验证码">
            <i slot="prefix" class="el-icon-collection-tag"/>
          </el-input>
        </el-col>
        <el-col :span="8">
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img"/>
<!--            <img :src="loginForm.image" class="login-code-img" @click="refreshCode"-->
            />
          </div>
        </el-col>
      </el-row>
    </el-form-item>
    <el-form-item prop="rememberMe">
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 10px 10px;">记住密码</el-checkbox>
    </el-form-item>
    <el-form-item>
      <el-button type="primary"
                 size="small"
                 :loading="loading"
                 @click.native.prevent="handleLogin"
                 class="login-submit">
        <span v-if="!loading">登 录</span>
        <span v-else>登 录 中...</span>
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  import {mapGetters} from "vuex";
  import website from '@/config/website';
  import { getCodeImg } from "@/api/login";
  import Cookies from "js-cookie";
  import { encrypt, decrypt } from '@/utils/jsencrypt'

  export default {
    name: "userlogin",
    data() {
      return {
        tenantMode: website.tenantMode,
        // captchaMode: website.captchaMode,
        codeUrl: "",
        loginForm: {
          //租户ID
          tenantId: "000000",
          //用户名
          username: "",
          //密码
          password: "",
          // 记住密码
          rememberMe: false,
          //账户类型
          type: "account",
          //验证码的值
          code: "",
          //验证码的索引
          uuid: "",
          //预加载白色背景
          image: "",
        },
        loginRules: {
          tenantId: [
            {required: false, message: "请输入租户ID", trigger: "blur"}
          ],
          username: [
            {required: true, message: "请输入用户名", trigger: "blur"}
          ],
          password: [
            {required: true, message: "请输入密码", trigger: "blur"},
            {min: 1, message: "密码长度最少为6位", trigger: "blur"}
          ]
        },
        loading: false,
        passwordType: "password",
        // 验证码开关
        captchaEnabled: true,
        // 注册开关
        register: false,
        redirect: undefined
      };
    },
    watch: {
      $route: {
        handler: function(route) {
          this.redirect = route.query && route.query.redirect;
        },
        immediate: true
      }
    },
    created() {
      this.getCode();
      this.getCookie();
    },
    computed: {
      ...mapGetters(["tagWel"])
    },
    methods: {
      // 获取验证码
      getCode() {
        getCodeImg().then(res => {
          this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
          if (this.captchaEnabled) {
            this.codeUrl = "data:image/gif;base64," + res.img;
            this.loginForm.uuid = res.uuid;
          }
        });
      },
      getCookie() {
        const username = Cookies.get("username");
        const password = Cookies.get("password");
        const rememberMe = Cookies.get('rememberMe')
        this.loginForm = {
          username: username === undefined ? this.loginForm.username : username,
          password: password === undefined ? this.loginForm.password : decrypt(password),
          rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
        };
      },
      // 查看密码
      showPassword() {
        this.passwordType === ""
          ? (this.passwordType = "password")
          : (this.passwordType = "");
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true;
            if (this.loginForm.rememberMe) {
              Cookies.set("username", this.loginForm.username, { expires: 30 });
              Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
              Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
            } else {
              Cookies.remove("username");
              Cookies.remove("password");
              Cookies.remove('rememberMe');
            }
            this.$store.dispatch("Login", this.loginForm).then(() => {
              this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
            }).catch(() => {
              this.loading = false;
              if (this.captchaEnabled) {
                this.getCode();
              }
            });
          }
        });
      },
    }
  };
</script>

<style>
</style>
